﻿using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.Presentation.Maps;
using SlamDunk.MES.Presentation.Models.NotifyModels;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单实际人员添加ViewModel
    /// </summary>
    public class AddActualPersonsViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IResourceContract> _iResourceContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public AddActualPersonsViewModel(ServiceProxy<IResourceContract> iResourceContract)
        {
            this._iResourceContract = iResourceContract;
        }
        #endregion

        #region 属性
        #region 人员列表
        /// <summary>
        /// 人员列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<IdentiferNameNm> PersonList { get; set; }
        #endregion

        #region 已选人员
        /// <summary>
        /// 已选人员
        /// </summary>
        [DependencyProperty]
        public IdentiferNameNm SelectedPerson { get; set; }
        #endregion

        #region 人员用途
        /// <summary>
        /// 人员用途
        /// </summary>
        [DependencyProperty]
        public string PersonUse { get; set; }
        #endregion

        #region 实际开始时间
        /// <summary>
        /// 实际开始时间
        /// </summary>
        [DependencyProperty]
        public DateTime ActualStartTime { get; set; }
        #endregion

        #region 实际结束时间
        /// <summary>
        /// 实际结束时间
        /// </summary>
        [DependencyProperty]
        public DateTime ActualEndTime { get; set; }
        #endregion

        #region 排序
        /// <summary>
        /// 排序
        /// </summary>
        [DependencyProperty]
        public int Sort { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion
        #endregion

        #region 方法
        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(Guid? facilityId)
        {
            ActualStartTime = DateTime.Now;
            ActualEndTime = DateTime.Now;
            PageModel<PersonInfo> Persons = await Task.Run(() => this._iResourceContract.Channel.GetPersonsByPage(
                null,//关键字
                facilityId,//工厂模型Id
                null, //人员类Id
                1,
                int.MaxValue));
            IEnumerable<IdentiferNameNm> Personsmodels = Persons.Datas.Select(x => x.ToIdentiferName());
            PersonList = new ObservableCollection<IdentiferNameNm>(Personsmodels);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证
            if (SelectedPerson == null)
            {
                MessageBox.Show("请选择人员信息！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (string.IsNullOrEmpty(PersonUse))
            {
                MessageBox.Show("请输入人员用途！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (ActualStartTime == null)
            {
                MessageBox.Show("请选择实际开始时间！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (ActualEndTime == null)
            {
                MessageBox.Show("请选择实际结束时间！", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            #endregion
            this.Busy();
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion
        #endregion

    }
}
